﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace DYSales.Views.Stock
{
    public partial class ciwn_addStock : ChildWindow
    {
        ObservableCollection<Web.StockAdd> sas;
        public ciwn_addStock()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ciwn_addStock_Loaded);
            tb_快速查询.KeyDown += new KeyEventHandler(tb_快速查询_KeyDown);
            bt_查询.Click += new RoutedEventHandler(bt_查询_Click);
            bt_显示所有.Click += new RoutedEventHandler(bt_显示所有_Click);
            bt_添加.Click += new RoutedEventHandler(bt_添加_Click);
            bt_删除.Click += new RoutedEventHandler(bt_删除_Click);
        }

        void bt_删除_Click(object sender, RoutedEventArgs e)
        {
            if (dg_ods.Selection.SelectedRows.Count == 1)
            {
                var od = dg_ods.Selection.SelectedRows.First().DataItem as Web.StockAdd;
                if (od != null)
                {
                    sas.Remove(od);
                }
            }
        }

        void bt_添加_Click(object sender, RoutedEventArgs e)
        {
            if (dg_pds.Selection.SelectedRows.Count != 1)
            {
                return;
            }
            if (tb_进货数量.Value > 0)
            {
                var pd = dg_pds.Selection.SelectedRows.First().DataItem as Web.PdTable;
                if (pd != null)
                {
                    dm.getDS().GetST(lo =>
                    {
                        var od = new Web.StockAdd();
                        od.st_操作员 = WebContext.Current.User.Name;
                        od.st_进货价 = Convert.ToDecimal(tb_进货价.Value);
                        od.st_进货时间 = lo.Value;
                        od.st_进货数量 =Convert.ToDecimal(tb_进货数量.Value);
                        od.st_商品编号 = pd.p_商品编号;
                        od.st_商品名称 = pd.p_商品名称;
                        od.st_批号 = stn_批号;
                        sas.Add(od);
                        tb_进货价.Value = 0;
                        tb_进货数量.Value = 0;
                    }, null);
                }
            }
        }

        void bt_显示所有_Click(object sender, RoutedEventArgs e)
        {
            dg_pds.DataContext = null;
            dg_pds.DataContext = dm.getDS().PdTables;
        }

        void bt_查询_Click(object sender, RoutedEventArgs e)
        {
            search();
        }

        void tb_快速查询_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                search();
            }
        }

        void search()
        {
            if (!string.IsNullOrEmpty(tb_快速查询.Text))
            {
                var pds = dm.getDS().PdTables.Where(p => p.p_条形码.Equals(tb_快速查询.Text));
                dg_pds.DataContext = null;
                dg_pds.DataContext = pds;
            }
        }

        string stn_批号;
        void ciwn_addStock_Loaded(object sender, RoutedEventArgs e)
        {
            dg_pds.AutoGeneratingColumn += other.GetOther().dg_AutoGeneratingColumn;
            dg_ods.AutoGeneratingColumn += other.GetOther().dg_AutoGeneratingColumn;
            dg_pds.DataContext = dm.getDS().PdTables;
            sas = new ObservableCollection<Web.StockAdd>();
            dg_ods.DataContext = sas;
            Unloaded += new RoutedEventHandler(ciwn_addStock_Unloaded);
            dm.getDS().GetST(lo =>
            {
                stn_批号 = lo.Value.Ticks.ToString();
            }, null);
        }

        void ciwn_addStock_Unloaded(object sender, RoutedEventArgs e)
        {
            dg_pds.AutoGeneratingColumn -= other.GetOther().dg_AutoGeneratingColumn;
            dg_ods.AutoGeneratingColumn -= other.GetOther().dg_AutoGeneratingColumn;
            sas = null;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (sas.Count > 0)
            {
                foreach (var item in sas)
                {
                    item.st_批号 = stn_批号;
                    decimal ntotal = item.st_进货价 * item.st_进货数量;
                    var pd = dm.getDS().PdTables.FirstOrDefault(p => p.p_商品名称.Equals(item.st_商品名称));
                    if (pd != null)
                    {
                        decimal ototal = pd.p_库存数量 * pd.p_进货价;
                        pd.p_库存数量 += item.st_进货数量;
                        pd.p_进货价 = (ntotal + ototal) / pd.p_库存数量;
                        dm.getDS().StockAdds.Add(item);
                    }
                }
                dm.getDS().SubmitChanges(le =>
                {
                    this.DialogResult = true;
                }, null);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            dm.getDS().RejectChanges();
            this.DialogResult = false;
        }
    }
}

